package cn.lb.interceptor;

import cn.lb.annotation.LBPermission;
import cn.lb.constant.LoginConstant;
import cn.lb.org.domain.Employee;
import cn.lb.permission.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.lang.reflect.Method;
import java.util.List;

@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Autowired
    private IPermissionService permissionService;
//    @Override
//    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//        String token = request.getHeader("token");
//        Employee employee =(Employee) LoginConstant.LoginUserMap.get(token);
//        if(ObjectUtils.isEmpty(employee)){
//            response.getWriter().print("{\"success\":false,\"message\":\"noLogin\"}");
//            return false;
//        }
//        if(handler instanceof HandlerMethod){
//            Method method = ((HandlerMethod) handler).getMethod();
//            LBPermission annotation = method.getAnnotation(LBPermission.class);
//            if (ObjectUtils.isEmpty(annotation)){
//                return true;
//            }
//            String sn=method.getDeclaringClass().getSimpleName()+":"+method.getName();
//            List<String> sns=permissionService.getsns(employee.getId());
//            if (!sns.contains(sn)){
//                response.getWriter().print("{\"success\":false,\"message\":\"noPermission\"}");
//                return false;
//            }
//        }
//        return true;
//    }
}
